import { Component, ElementRef, OnInit, Output, Input, ViewChild, AfterViewInit, EventEmitter } from '@angular/core';
import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';

import { RouterModule, Router, ActivatedRoute } from '@angular/router';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { Service } from '../../../server/server';
import { Pagination, PaginationUrl } from '../../../public/serve/pcrudInfo';
import { addDatatype } from '../../institution/institution-add-admin/institution-add-admin.component';
declare var $: any;

@Component({
  selector: 'app-management-table',
  templateUrl: './management-table.component.html'
})

export class ManagementTableComponent implements OnInit {

  @Input() tableNumber: number;

  right: any = {
    "user": {},
    "root_user": {}
  };
  constructor(private tableListService: CudTableService, private removeTableService: RemoveTableService, private server: Service) {
    this.right = $.extend(this.right, server.getRight());
  }

  ngOnInit() {
    this.getTabList(1, 20, '0', 2, '', '', '', '', '')

    //接受查询结果
    this.managementsearcheventEmit = this.server.managementsearcheventEmit.subscribe((value: any) => {
      this.tabVal = value.result;
      //console.log(this.tabVal)
    });
  }

  //取消订阅
  public managementsearcheventEmit: any;
  ngOnDestroy() {
    if (this.managementsearcheventEmit) {
      this.managementsearcheventEmit.unsubscribe();
    }
  }

  public tabVal: any;
  @Output()
  tabeListOutput: EventEmitter<string> = new EventEmitter();

  public userId: string;

  // 表格
  getTabList(pageNumber, pageSize, id, queryType, queryCriteria, startCreateTime, endCreateTime, startLoginTime, endLoginTime) {
    let url = this.server.baseUrl+'admin/pusers/getPusersList';
    //let data = new Pagination(pageNumber, pageSize, id, queryType);
    let data = {
      pageNumber: pageNumber,
      pageSize: pageSize,
      id: id || '12aa1c87305f4271a90bffeb28315ca2',
      queryType: queryType,
      queryCriteria: queryCriteria,
      startCreateTime: startCreateTime,
      endCreateTime: endCreateTime,
      startLoginTime: startLoginTime,
      endLoginTime: endLoginTime
    }
    //console.log(data)
    this.tableListService.postRequest(url, data).then((data: any) => {
      if (data.result_code == '0000') {
        this.tabVal = data.result;
        this.tabeListOutput.emit(data.length);
      }
      // console.log(data);
    }).catch();
  }

  @Output()
  detailsOutput: EventEmitter<any> = new EventEmitter();


  //所属机构
  theshow(val) {
    $("#the").modal("show");
    this.server.managementtheeventEmit.emit(val);
  }

  // 详情（弹窗），用于展示默认数据
  detailsPopup(tabval) {
    $("#details").modal("show");
    this.outputTabVal(tabval);
    this.server.managementeventEmit.emit(tabval.userId);
    // console.log(tabval)
  }

  // 修改（弹窗），用于展示默认数据
  editPopup(tabval) {
    $("#update").modal("show");
    this.outputTabVal(tabval);
    this.server.managementUpdateeventEmit.emit(tabval.userId);
    //console.log(tabval)
  }
  //修改密码
  updatePassword(tabval) {
    $("#password").modal("show");
    this.outputTabVal(tabval);
  }
  //重置密码
  restPassword(tabval) {
    $("#passwordReset").modal("show");
    this.outputTabVal(tabval);
  }

  // 每一行的表格信息
  outputTabVal(tabval) {
    let tabVal = {
      userName: tabval.userName,
      displayName: tabval.displayName,
      email: tabval.email,
      createDate: tabval.createDate,
      status: tabval.status,
      institutionId: tabval.institutionId,
      userId: tabval.userId,
      roleId: tabval.roleId,
      lastLoginDate: tabval.lastLoginDate,
      password: tabval.password,
      userFolder: tabval.userFolder,
      portrait: tabval.portrait,
      isActivate: tabval.isActivate,
      isAutonym: tabval.isAutonym,
      sex: tabval.sex,
      updatePasswordDate: tabval.updatePasswordDate,
      registerSource: tabval.registerSource,
      roleName: tabval.roleName,
      userInfos: tabval.userInfos,
      isSuper: tabval.isSuper,
      activateKey: tabval.activateKey,
      activateLastDate: tabval.activateLastDate

    }
    this.detailsOutput.emit(tabVal);
  }

  // 全选
  public isSelectAll: boolean = false;
  public isChose: boolean = false;
  public isSelect: boolean = true;
  public isDisable: any;              // 按钮（启用/禁用）

  @Output() isSelectFromTable = new EventEmitter<boolean>();

  selectAll(bool, tabVal) {
    this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'userId');
    this.isSelect = !this.isSelectAll;
    this.isSelectFromTable.emit(this.isSelect);
  }

  // 单选（多选）
  chose(bool, userId, length) {
    this.isDisable = this.removeTableService.removeChose(bool, userId, length);
    this.isSelectAll = this.isDisable.bool;
    this.isDisable.len ? this.isSelect = false : this.isSelect = true;
    this.isSelectFromTable.emit(this.isSelect);
  }

  // 没有数据时取消勾选全选按钮
  cancelSelectAll() {
    this.isSelectAll = false;
    this.isChose = false;
  }
}